//
// Created by 紫霄 on 2020/4/20.
//

#ifndef LEETCODE_32_H
#define LEETCODE_32_H
#include <queue>
#include <vector>
using namespace std;
struct TreeNode {
  int val;
  TreeNode *left;
  TreeNode *right;
  TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class Solution {
public:
  vector<vector<int>> ans;
  vector<vector<int>> levelOrder(TreeNode *root) {
    //    vector<vector<int>> ans;
    if (root) {
      vector<int> rootVec;
      queue<TreeNode *> rootNode;
      rootNode.push(root);
      everyLevel(rootNode, rootVec);
    }
    return ans;
  }
  void everyLevel(queue<TreeNode *> curNodes, vector<int> &curLevel) {
    queue<TreeNode *> nextNodes;
    while (!curNodes.empty()) {
      TreeNode *curPosi = curNodes.front();
      if (curPosi->left) {
        nextNodes.push(curPosi->left);
      }
      if (curPosi->right) {
        nextNodes.push(curPosi->right);
      }
      curLevel.push_back(curPosi->val);
      curNodes.pop();
    }
    ans.push_back(curLevel);
    if (!nextNodes.empty()) {
      vector<int> nextLevel;
      everyLevel(nextNodes, nextLevel);
    }
  }
};
#endif // LEETCODE_32_H
